package com.hjw.service;

import com.spring.BeanPostProcessor;
import com.spring.Component;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;


@Component("hjwBeanPostProcessor")
public class HjwBeanPostProcessor implements BeanPostProcessor {
    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) {

        if(beanName.equals("userService")){
            System.out.println("初始化前");
            ((UserServiceImpl)bean).setBeanName("是Uservice");
        }
        return bean;
    }

    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) {

        if("userService".equals(beanName)){
            System.out.println("初始化后");
            Object proxyInstance = Proxy.newProxyInstance(HjwBeanPostProcessor.class.getClassLoader(),
                    bean.getClass().getInterfaces(),new InvocationHandler(){
                        @Override
                        public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                            System.out.println("代理逻辑");

                            return  method.invoke(bean,args);
                        }
                    });
            return proxyInstance;
        }
       return bean;

    }
}
